/*
 * RoleControl.java
 *
 * Created on 22 January 2004, 02:31
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.role;

import com.velocityme.client.gui.ChangeDeltaPanel;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.RolePK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.PermissionValue;
import com.velocityme.valueobjects.RoleValue;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert Crida Work
 */
public class RoleControl extends javax.swing.JPanel {
    
    private RolePK m_rolePK;
    private NodePK m_parentNodePK;
    private KeySession m_key;
    private RoleValueDisplay m_roleValueDisplay;
    private boolean m_editing = true;
    private Set m_setPermissions;
    private Set m_allPermissionValues = null;
    private Set m_assignedPermissionValues = null;

    /** Creates new form RoleControl */
    public RoleControl(KeySession p_key, RolePK p_rolePK, boolean p_editing, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_rolePK = p_rolePK;
        m_editing = p_editing;
        m_setPermissions = p_setPermissions;
        try {
            m_assignedPermissionValues = StatelessRemoteClientSessionUtil.getSession().getRoleAssignedPermissionValues(m_key, m_rolePK);
            m_allPermissionValues = StatelessRemoteClientSessionUtil.getSession().getAllPermissionValues(m_key);
            m_roleValueDisplay = new RoleValueDisplay(StatelessRemoteClientSessionUtil.getSession().getRoleValue(m_key,m_rolePK), m_assignedPermissionValues, m_allPermissionValues);
            if (!p_editing) {
                jTabbedPane1.addTab("Change Log", new ChangeDeltaPanel(m_key, new NodePK(m_rolePK.getRoleId())));
                jButtonEdit.setEnabled(m_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ROLE_EDIT))));
            }
            else {
                jButtonOK.setEnabled(true);
                jButtonCancel.setEnabled(true);
            }
            m_roleValueDisplay.setEditable(p_editing);
            jPanel1.add(m_roleValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Role", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** Creates new form RoleControl */
    public RoleControl(KeySession p_key, NodePK p_parentNodePK) {
        initComponents();
        m_key = p_key;
        m_parentNodePK = p_parentNodePK;
        try {
            m_assignedPermissionValues = new HashSet();
            m_allPermissionValues = StatelessRemoteClientSessionUtil.getSession().getAllPermissionValues(m_key);
            m_roleValueDisplay = new RoleValueDisplay(new RoleValue(), m_assignedPermissionValues, m_allPermissionValues);
            m_roleValueDisplay.setEditable(true);
            jPanel1.add(m_roleValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        jButtonOK.setEnabled(true);
        jButtonCancel.setEnabled(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonEdit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanel1ComponentShown(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout());

        jButtonEdit.setText("Edit");
        jButtonEdit.setEnabled(false);
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonEdit);

        jButtonOK.setText("OK");
        jButtonOK.setEnabled(false);
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.setEnabled(false);
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.addTab("Role", jPanel1);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            RoleValue roleValue = m_roleValueDisplay.getRoleValue();
            Set permissionPKs = new HashSet();
            Iterator i = m_roleValueDisplay.getAssignedPermissionValues().iterator();
            while(i.hasNext())
                permissionPKs.add(((PermissionValue)i.next()).getPrimaryKey());
            if (roleValue.roleIdHasBeenSet())
                StatelessRemoteClientSessionUtil.getSession().roleEdit(m_key, m_rolePK, roleValue, permissionPKs);
            else
                StatelessRemoteClientSessionUtil.getSession().roleCreate(m_key, m_parentNodePK, roleValue, permissionPKs);
            getParent().remove(this);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        RoleControl editPanel = new RoleControl(m_key, m_rolePK, true, m_setPermissions);
        ((JTabbedPane)getParent()).addTab("Edit Role", editPanel);
        ((JTabbedPane)getParent()).setSelectedComponent(editPanel);        
    }//GEN-LAST:event_jButtonEditActionPerformed

    private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel1ComponentShown
        // Add your handling code here:
        try {
            if (!m_editing)
                m_roleValueDisplay.setRoleValue(StatelessRemoteClientSessionUtil.getSession().getRoleValue(m_key,m_rolePK), m_assignedPermissionValues, m_allPermissionValues);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }                
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }                
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }                
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Role", JOptionPane.WARNING_MESSAGE);
        }        
    }//GEN-LAST:event_jPanel1ComponentShown
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
